package com.foyekoo.project001.fragment;

import android.annotation.SuppressLint;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;

import com.foyekoo.project001.R;
import com.foyekoo.project001.databinding.FragmentBaseSetBinding;
import com.foyekoo.project001.model.ClientBaseSetVM;
import com.foyekoo.project001.utils.ByteUtil;
import com.foyekoo.project001.utils.ReadThread;
import com.foyekoo.project001.utils.ToastUtils;

public class BaseSetFragment extends BaseFragment implements View.OnTouchListener {
    private static final String TAG = "BaseSetFragment";

    FragmentBaseSetBinding binding;

    ClientBaseSetVM clientBaseSetVM;


    @SuppressLint("ClickableViewAccessibility")
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {





        binding = DataBindingUtil.inflate(inflater,R.layout.fragment_base_set,null,false);
        clientBaseSetVM = new ClientBaseSetVM();
        binding.setBasesetFragmentVM(clientBaseSetVM);
        getActivity().registerReceiver(dataReceiveBr,new IntentFilter(ReadThread.ACTION_DATA_RECIEVE));

        binding.btnBack.setOnTouchListener(this);
        binding.btnPush.setOnTouchListener(this);
        binding.btnLeft.setOnTouchListener(this);
        binding.btnRight.setOnTouchListener(this);
        binding.btnUp.setOnTouchListener(this);
        binding.btnDown.setOnTouchListener(this);
        binding.btnDownMax.setOnTouchListener(this);
        binding.btnUpMax.setOnTouchListener(this);



        return binding.getRoot();

    }


    /**
     * 广播
     */
    private BroadcastReceiver dataReceiveBr = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {

            byte[] data = intent.getByteArrayExtra(ReadThread.EXTRA_DATA);


            if(data[2] == 0x05) { //按下按键

                byte key = data[3];
                keyDown(key);
            } else if (data[2] == 0x06) { //松开按键
                byte key = data[3];
                keyUp(key);
            } else if (data[2] == 0x09) {
                updateMarchPosition(data);
            } else if (data[2] == 0x07){
                //投币测试

                byte[] coinData = new byte[2];
                coinData[0] = data[4];
                coinData[1] = data[5];

                int coin = Integer.parseInt(ByteUtil.byte2String(coinData),16);

                clientBaseSetVM.coinTestNum.set(coin + "");
            }

        }
    };

    /**
     * 更新电机的值
     */
    private void updateMarchPosition(byte[] data){
        if (data[3] == 0x00) { //左右电机

            clientBaseSetVM.leftRightMachineryPosition.set(data[4] + "");

        } else if (data[3] == 0x01) { //上下电机
            clientBaseSetVM.upDownMachineryPosition.set(data[4] + "");
        } else if (data[3] == 0x02)  { //前后电机


            byte[] maxHeight = new byte[2];
            maxHeight[0] = data[4];
            maxHeight[1] = data[5];

            clientBaseSetVM.frontBehindMachineryPosition.set(Integer.parseInt(ByteUtil.byte2String(maxHeight),16) + "");
        }
    }

    private void keyDown(byte key){
        if (key == 0x00){ //开始键
            clientBaseSetVM.startKeyIsClick.set(true);
        } else if(key == 0x01) {
            clientBaseSetVM.toRightKeyIsClick.set(true);
        } else if (key == 0x02) {
            clientBaseSetVM.frettingDownKeyIsClick.set(true);
        } else if(key == 0x03) {
            clientBaseSetVM.frettingleftKeyIsClick.set(true);
        } else if (key == 0x04) {
            clientBaseSetVM.frettingRightKeyIsClick.set(true);
        } else if (key == 0x05) {
            clientBaseSetVM.menuKeyIsClick.set(true);
        } else if(key == 0x06) {
            clientBaseSetVM.fiveGameKeyIsClick.set(true);
        } else if (key == 0x07) {
            clientBaseSetVM.inCoinKeyIsClick.set(true);
        } else if (key == 0x08 ){
            clientBaseSetVM.confirmKeyIsClick.set(true);
        } else if (key == 0x09) {
            clientBaseSetVM.upKeyIsClick.set(true);
        } else if(key == 0x0A) {
            clientBaseSetVM.downKeyIsClick.set(true);
        } else if(key == 0x0b) {
            clientBaseSetVM.leftKeyIsClick.set(true);
        } else if (key == 0x0c) {
            clientBaseSetVM.rightKeyIsClick.set(true);
        } else if (key == 0x0d) {
            clientBaseSetVM.dropProductEyes.set(true);
        }
    }

    private void keyUp(byte key){
        if (key == 0x00){ //开始键
            clientBaseSetVM.startKeyIsClick.set(false);
        } else if(key == 0x01) {
            clientBaseSetVM.toRightKeyIsClick.set(false);
        } else if (key == 0x02) {
            clientBaseSetVM.frettingDownKeyIsClick.set(false);
        } else if(key == 0x03) {
            clientBaseSetVM.frettingleftKeyIsClick.set(false);
        } else if (key == 0x04) {
            clientBaseSetVM.frettingRightKeyIsClick.set(false);
        } else if (key == 0x05) {
            clientBaseSetVM.menuKeyIsClick.set(false);
        } else if(key == 0x06) {
            clientBaseSetVM.fiveGameKeyIsClick.set(false);
        } else if (key == 0x07) {
            clientBaseSetVM.inCoinKeyIsClick.set(false);
        } else if (key == 0x08 ){
            clientBaseSetVM.confirmKeyIsClick.set(false);
        } else if (key == 0x09) {
            clientBaseSetVM.upKeyIsClick.set(false);
        } else if(key == 0x0A) {
            clientBaseSetVM.downKeyIsClick.set(false);
        } else if(key == 0x0b) {
            clientBaseSetVM.leftKeyIsClick.set(false);
        } else if (key == 0x0c) {
            clientBaseSetVM.rightKeyIsClick.set(false);
        } else if (key == 0x0d) {
            clientBaseSetVM.dropProductEyes.set(false);
        }
    }


    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            if (v.getId() == R.id.btn_left) {
                clientBaseSetVM.leftMachinery(2);
            } else if (v.getId() == R.id.btn_right) {
                clientBaseSetVM.rightMachinery(1);
            } else if (v.getId() == R.id.btn_up || v.getId() == R.id.btn_up_max) {
                clientBaseSetVM.upMachinery(1);
            } else if (v.getId() == R.id.btn_down || v.getId() == R.id.btn_down_max) {
                clientBaseSetVM.downMachinery(2);
            } else if (v.getId() == R.id.btn_push) {
                clientBaseSetVM.frontMachinery(1);
            } else if (v.getId() == R.id.btn_back) {
                clientBaseSetVM.behindMachinery(2);
            }


        } else if (event.getAction() == MotionEvent.ACTION_UP) {
            if (v.getId() == R.id.btn_left) {
                clientBaseSetVM.leftMachinery(0);
            } else if (v.getId() == R.id.btn_right) {
                clientBaseSetVM.rightMachinery(0);
            } else if (v.getId() == R.id.btn_up) {
                clientBaseSetVM.upMachinery(0);
            } else if (v.getId() == R.id.btn_down) {
                clientBaseSetVM.downMachinery(0);
            } else if (v.getId() == R.id.btn_push) {
                clientBaseSetVM.frontMachinery(0);
            } else if (v.getId() == R.id.btn_back) {
                clientBaseSetVM.behindMachinery(0);
            } else if (v.getId() == R.id.btn_down_max || v.getId() == R.id.btn_up_max) {
                clientBaseSetVM.upHeightMachinery();
            }
        }

        return false;
    }


}
